package domain.extendMappingSingleTable;


import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;


public class test {

    public static void main(String[] args){
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        Employee employee1 = new Employee("e1",'f',"e1t",1.0);
        Employee employee2 = new Employee("e2",'m',"e2t",2.0);

        Manager manager = new Manager("m1",'f',"depart1");

        Customer customer1 = new Customer("c1",'m',"comments");
        Customer customer2 = new Customer("c2",'f',"comments2");
        session.persist(customer1);
        session.persist(customer2);

        //对于1-N无连接表时可以不使用mappedBy
        // 事务的顺序需要严格执行，从N端设置对应的1端实体
        customer1.setEmployee(employee1);
        customer2.setEmployee(employee1);
        session.persist(employee1);
        session.persist(employee2);
        employee1.setManager(manager);
        employee2.setManager(manager);
        session.persist(manager);

        transaction.commit();
        session.close();
        sessionFactory.close();



    }
}
